<template>
  <div>
    <el-breadcrumb separator-class="el-icon-arrow-right" class="nav">
      <el-breadcrumb-item to="/welcome"
        ><strong>首页</strong></el-breadcrumb-item
      >
      <el-breadcrumb-item>商品管理</el-breadcrumb-item>
      <el-breadcrumb-item>参数列表 </el-breadcrumb-item>
    </el-breadcrumb>
    <el-card class="box-card">
      <el-alert
        title="注意：只允许为第三级分类设置相关参数！"
        type="warning"
        :closable="false"
        show-icon
      >
      </el-alert>
      <div class="goods">
        <span>选择商品分类:</span>
        &nbsp;
        <el-cascader
          v-model="value"
          :options="list"
          clearable
          size="medium"
          :props="{
            expandTrigger: 'hover',
            label: 'cat_name',
            value: 'cat_id',
          }"
          @change="handleChange"
        ></el-cascader>
      </div>

      <el-tabs v-model="activeName" @tab-click="handletableClick">
        <el-tab-pane label="动态参数" name="many">
          <el-button
            type="primary"
            @click="add"
            :disabled="value.length < 3"
            class="btn"
            >添加参数</el-button
          >
          <el-table :data="manylist" class="table" border style="width: 100%">
            <el-table-column type="expand" width="50">
              <template slot-scope="{ row }">
                <el-tag
                  closable
                  @close="handleClose(row, index)"
                  v-for="(item, index) in row.attr_vals"
                  :key="index"
                  >{{ item }}
                </el-tag>
                <!--自定义tag标签-->
                <el-input
                  class="input-new-tag"
                  v-if="row.inputVisible"
                  v-model="row.inputValue"
                  ref="saveTagInput"
                  size="small"
                  @keyup.enter.native="handleInputConfirm(row)"
                  @blur="handleInputConfirm(row)"
                >
                </el-input>
                <el-button
                  v-else
                  class="button-new-tag"
                  size="small"
                  @click="showInput(row)"
                  >+ New Tag</el-button
                >
              </template>
            </el-table-column>
            <el-table-column label="#" type="index" width="50">
            </el-table-column>
            <el-table-column prop="attr_name" label="属性名称">
            </el-table-column>
            <el-table-column prop="name" label="操作">
              <template slot-scope="{ row }">
                <div class="actions">
                  <el-button type="primary" @click="edit(row)">
                    <i class="el-icon-edit-outline"></i>编辑</el-button
                  >
                  <el-button type="danger">
                    <i class="el-icon-delete" @click="del(row)"></i
                    >删除</el-button
                  >
                </div>
              </template>
            </el-table-column>
          </el-table></el-tab-pane
        >
        <el-tab-pane label="静态属性" name="only">
          <el-button
            type="primary"
            @click="add"
            :disabled="value.length < 3"
            class="btn"
            >添加属性</el-button
          >
          <el-table :data="onlylist" class="table" border style="width: 100%">
            <el-table-column type="expand" width="50">
              <template slot-scope="{ row }">
                <el-tag
                  closable
                  @close="handleClose(row, index)"
                  v-for="(item, index) in row.attr_vals"
                  :key="index"
                  >{{ item }}
                </el-tag>
                <!--自定义tag标签-->
                <el-input
                  class="input-new-tag"
                  v-if="row.inputVisible"
                  v-model="row.inputValue"
                  ref="saveTagInput"
                  size="small"
                  @keyup.enter.native="handleInputConfirm(row)"
                  @blur="handleInputConfirm(row)"
                >
                </el-input>
                <el-button
                  v-else
                  class="button-new-tag"
                  size="small"
                  @click="showInput(row)"
                  >+ New Tag</el-button
                >
              </template>
            </el-table-column>
            <el-table-column label="#" type="index" width="50">
            </el-table-column>
            <el-table-column prop="attr_name" label="静态属性">
            </el-table-column>
            <el-table-column prop="name" label="操作">
              <template slot-scope="{ row }">
                <div class="actions">
                  <el-button type="primary" @click="edit(row)">
                    <i class="el-icon-edit-outline"></i>编辑</el-button
                  >
                  <el-button type="danger">
                    <i class="el-icon-delete" @click="del(row)"></i
                    >删除</el-button
                  >
                </div>
              </template>
            </el-table-column>
          </el-table>
        </el-tab-pane>
      </el-tabs>
      <!--打开添加参数/属性动态库-->
      <el-dialog
        @close="close"
        :title="'添加' + title"
        :visible.sync="dialogFormVisible"
      >
        <el-form :model="form" ref="addForm" :rules="rules">
          <el-form-item :label="title" prop="name" label-width="120px">
            <el-input v-model="form.name" autocomplete="off"></el-input>
          </el-form-item>
        </el-form>
        <div slot="footer" class="dialog-footer">
          <el-button @click="dialogFormVisible = false">取 消</el-button>
          <el-button type="primary" @click="quedingEdit">确 定</el-button>
        </div>
      </el-dialog>
    </el-card>
  </div>
</template>
<script>

import { GetGoodListApi, goodsprop, editpropApi, addparams } from '@/api/goods'
export default {
  name: '',
  data () {
    return {
      value: [],
      list: [],
      activeName: 'many',
      manylist: [],
      onlylist: [],
      dialogFormVisible: false,
      form: {
        name: ''
      },
      rules: {
        name: [{ required: true, message: '请输入内容', trigger: 'blur' }]
      }
    }
  },
  created () {
    this.getgoodslist()
  },
  methods: {
    // tabs栏点击时
    handletableClick (tab, event) {
      this.getdataprop()
    },

    // 级联选择器发生变化时
    handleChange () {
      this.getdataprop()
    },

    async getgoodslist () {
      const res = await GetGoodListApi()
      this.list = res.data
      console.log(res, '获取-商品分类列表')
    },

    // 点击tab标签时,获取对应的商品数据
    async getdataprop () {
      if (this.value.length < 3) {
        this.govalue = []
        return
      }
      // console.log(this.value[2],);
      const res = await goodsprop(this.value[2], this.activeName)
      res.data.forEach(item => {
        item.attr_vals === '' ? item.attr_vals = [] : item.attr_vals = item.attr_vals.split(' ')
        // 给每一个文本框添加值
        item.inputVisible = false
        item.inputValu = ''
      })
      if (this.activeName === 'many') {
        this.manylist = res.data
      } else if (this.activeName === 'only') {
        this.onlylist = res.data
      }
    },

    // 点击删除标签
    handleClose (row, index) {
      row.attr_vals.splice(index, 1)
      this.geteditpropApi(row)
    },

    // tag文本框失去焦点,或者点击enter触发
    handleInputConfirm (row) {
      console.log(row.inputValue);
      // console.log(row.inputVisible);
      if (row.inputValue === undefined || row.inputValue.trim().length === 0) {
        row.inputValue = ''
        return row.inputVisible = false
      }
      row.attr_vals.push(row.inputValue.trim())
      this.geteditpropApi(row)
      row.inputValue = ''
      row.inputVisible = false
    },

    async geteditpropApi (row) {
      await editpropApi(this.value[2], row.attr_id, row.attr_name, row.attr_sel, row.attr_vals.join(' '))
      this.$message.success('修改成功')
    },
    edit () { },
    del () { },
    // tag按钮点击事件
    showInput (row) {
      row.inputVisible = true
      this.$nextTick(_ => {
        this.$refs.saveTagInput.$refs.input.focus()
      })
    },
    // 打开添加参数对话框
    async add () {
      this.dialogFormVisible = true
    },
    // 确定添加参数
    quedingEdit () {
      this.$refs.addForm.validate(async (valid) => {
        if (valid) {
          await addparams(this.value[2], this.form.name, this.activeName)
          this.$message.success('添加成功')
          this.getdataprop()
          this.dialogFormVisible = false
        } else {
          // 不满足则提示用户输入
          this.$message.error('请输入' + this.title)
          return false
        }
      })
    },
    // 关闭dialog对话框,清空表单值
    close () {
      this.$refs.addForm?.resetFields()
    }
  },
  computed: {
    title () {
      return this.activeName === 'many' ? '动态参数' : '静态属性'
    }
  },


}
</script>
<style lang='less'  scoped>
.goods {
  margin: 20px 0;
}
.nav {
  font-size: 12px;
  margin-bottom: 15px;
}
.table {
  font-size: 12px;
}

.actions {
  display: flex;
  button {
    display: flex;
    justify-content: center;
    align-items: center;
    width: 70px;
    height: 30px;
  }
}

.btn {
  width: 80px;
  height: 28px;
  line-height: 28px;
  color: white;
  font-size: 12px;
  display: flex;
  justify-content: center;
  align-items: center;
  margin-bottom: 10px;
}

.el-tag + .el-tag {
  margin: 0 5px 0 10px;
}
.button-new-tag {
  margin-left: 10px;
  height: 32px;
  line-height: 30px;
  padding-top: 0;
  padding-bottom: 0;
}
.input-new-tag {
  width: 90px;
  margin-left: 10px;
  vertical-align: bottom;
}
</style>
